function Enemy(position,map) {
    this.type = 0;
    this.optimized = false;
    this.setPosition(position);
    this.setMap(map);
    this.speed = 1;
    this.life = 200;
    this.draggable = false;
    this.icon = "./img/enemy1.gif";
    this.title = this.life;
    this.infoWindow = new google.maps.InfoWindow({content: "", maxWidth:50});
    this.currentPositionInPath = 0;
    this.label = null;
    this.valueOfKilling = 25;

    this.setLife = function(life) {
        this.life = life;
    };

    this.reduceLifeIn = function(life) {
        this.life = this.life - life;
    };

    this.setEnemyPosition = function(latlng) {
        this.setPosition(latlng);
    };

    this.removeFromMap = function() {
        this.label.setMap(null);
        this.setMap(null);
    }

    
};

Enemy.prototype = new google.maps.Marker();

//------------------- Label to show life ----------------//

// Define the label to show life status, derived from google.maps.OverlayView
function Label(opt_options, maxLife, textOfBar) {
    this.textOfBar = textOfBar;

    // Initialization
    this.setValues(opt_options);
   
    // Label specific
    var div = this.div_ = document.createElement('div');

    // create progress bar  ;
    var bar = this.bar_ = document.createElement('progress');
    bar.setAttribute("value", '100%');
    bar.setAttribute("max", maxLife);
    bar.style.cssText = 'position: relative; left: -50%; top: 8px; width:80px; height:10px;';
    div.appendChild(bar);

    div.appendChild(document.createElement('br'));

    var span = this.span_ = document.createElement('span');
    span.style.cssText = 'position: relative; left: -25%; top: -8px; ' +
                         'white-space: nowrap; border: 1px clear; ' +
                         'padding: 2px; background-color: clear;';
    div.appendChild(span);
    div.style.cssText = 'position: absolute; display: none; ';
};

Label.prototype = new google.maps.OverlayView;
   
// Implement onAdd
Label.prototype.onAdd = function() {
    var pane = this.getPanes().overlayLayer;
    pane.appendChild(this.div_);

    // Ensures the label is redrawn if the text or position is changed.
    var me = this;
    this.listeners_ = [
      google.maps.event.addListener(this, 'position_changed',
          function() { me.draw(); }),
      google.maps.event.addListener(this, 'life_changed',
          function() { me.draw(); })
      ];
};
   
// Implement onRemove
Label.prototype.onRemove = function() {
    this.div_.parentNode.removeChild(this.div_);

    // Label is removed from the map, stop updating its position/text.
    for (var i = 0, I = this.listeners_.length; i < I; ++i) {
      google.maps.event.removeListener(this.listeners_[i]);
    }
};
   
// Implement draw
Label.prototype.draw = function() {
    var projection = this.getProjection();
    var position = projection.fromLatLngToDivPixel(this.get('position'));

    var div = this.div_;
    if (div) {
        div.style.left = position.x + 'px';
        div.style.top = position.y + 'px';
        div.style.display = 'block';
    }
    

    this.span_.innerHTML = this.textOfBar + ": " + this.get('life').toString();
    this.bar_.setAttribute("value", this.get('life').toString()); 
    this.bar_.innerHTML = this.get('life').toString();
};

//------------------- EnemyFactory ----------------//

function EnemyFactory() {
};

EnemyFactory.createEnemy = function(type, map, position) {
    enemy = new Enemy(position,map);
    enemy.type = type;

    if (type == 1) {
        enemy.speed = 1;
        enemy.life = 200;
        enemy.icon = "./img/enemy1.gif";
    } else if (type == 2) {
        enemy.speed = 2;
        enemy.life = 100;
        enemy.icon = "./img/enemy2.gif";
    } else if (type == 3) {
        enemy.speed = 3;
        enemy.life = 75;
        enemy.icon = "./img/enemy3.gif";
    }

    // set lifeLabel
    
    label = new Label({map: enemy.map}, enemy.life, "vida");
    label.bindTo('position', enemy, 'position');
    label.bindTo('life', enemy, 'life');
    enemy.label = label;
    return enemy;
    
};